import { baseUrl, codeKey } from '../config/config'
import { decrypt } from '@/util/util'
import store from '@/store'
export default function request(successCallBack, errorCallBack, url, method, param, noLoading, header) {
  if (!noLoading) {
    uni.showLoading({
      title: '加载中'
    })
  }
  header = header == null ? {
    'Authorization': 'Bearer' + ' ' + store.getters.uniToken,
    // 'Content-type': 'application/x-www-form-urlencoded'
    'Content-Type': 'application/json; charset=utf-8'
  } : {
    'Authorization': 'Bearer' + ' ' + store.getters.uniToken,
    'Content-type': 'application/x-www-form-urlencoded'
    // 'Content-Type': 'application/json; charset=utf-8'
  }
  uni.request({
    url: url, //仅为示例，并非真实接口地址。
    data: param,
    header: header,
    method: method,
    success: (data) => {
      // console.log(data)
      // if (decrypt(data.data, codeKey)) {
      //   data.data = JSON.parse(decrypt(data.data, codeKey))
      // }
      if (data.data.code == 401) {
        uni.showToast({
          icon: 'none',
          title: 'token已失效，请重新登录!'
        })
        store.commit('user/SET_TOKEN', '')
        store.commit('user/SET_USER_INFO', {})
        store.commit('user/SET_ROLE', [])
        uni.reLaunch({
          url: '/pages/user/login'
        })
      }
      // console.log(data)
      //成功处理
      if (null != successCallBack) {
        successCallBack(data)
      }
    },
    fail: (error) => {
      //异常处理；
      if (null != errorCallBack) {
        errorCallBack(xhr, textStatus, errorThrown)
      }
    },
    complete: () => {
      uni.hideLoading()
    }
  })
}